<template>

  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img
          src="../../assets/logo/2.png"
          alt=""
          width="18%"
        >
        <span>
          后台管理系统
        </span>
      </div>
      <el-button
        type="info"
        @click="logout"
      >注销</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px':'200px'">
        <!-- 侧边栏菜单 -->
        <div
          class="toggle-button"
          @click="toggleCollapse"
        >|||</div>
        <el-menu
          background-color="rgb(25, 33, 41)"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse-transition="false"
          :collapse="isCollapse"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            v-for="item in menulist"
            :key="item.id"
            :index="item.id + ''"
          >
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]" />
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="subItem in item.children"
              :key="subItem.id"
              :index="'/' + subItem.path "
              @click="saveNavState('/'+ subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu" />
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menulist: [
        { id: 1,
          authName: '用户管理',
          children: [{
            id: 1,
            authName: '管理员',
            path: 'adminControl'
          }, {
            id: 2,
            authName: '用户',
            path: 'userControl'
          }]
        },
        { id: 2,
          authName: '发布信息管理',
          children: [{
            id: 1,
            authName: '出售商品',
            path: 'sell'
          }, {
            id: 2,
            authName: '采购需求',
            path: 'buy'
          }, {
            id: 3,
            authName: '校园活动',
            path: 'activities'
          }, {
            id: 4,
            authName: '校园吐槽',
            path: 'complaints'
          }]
        }
      ],
      iconsObj: {
        1: 'iconfont icon-users',
        2: 'iconfont icon-tijikongjian',
        3: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      /* 是否折叠 */
      isCollapse: false,
      /* 被激活的地址 */
      activePath: ''
    }
  },
  created() {
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/admin')
    },
    /* 获取菜单 */

    /* 控制侧边菜单栏展开 */
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang='less' scoped>
.el-header {
  background-color: rgb(45, 50, 117);
  display: flex;
  justify-content: space-between;
  padding-left: 5px;
  padding-top: 1px;
  align-items: center;
  color: white;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 10px;
    }
  }
}
.el-aside {
  background-color: rgb(25, 33, 41);
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: rgb(224, 225, 235);
}
.home_container {
  height: 100%;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background: rgb(51, 60, 70);
  font-size: 10px;
  line-height: 24px;
  color: white;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
